package com.dm.library.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.dm.library.R;
import com.dm.library.widgets.element.DTextView;
import com.dm.library.widgets.keyboard.Keyboard;
import com.dm.library.widgets.keyboard.PayEditText;


/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  选择 拍照 相片
 * Created by tangjian on 2016/11/22.
 */

public class InputSmsCodeDialog extends Dialog implements View.OnClickListener {

    private static final String[] KEY = new String[]{
            "1", "2", "3",
            "4", "5", "6",
            "7", "8", "9",
            "<<", "0", "ok"
    };


    private View view;
    private final Keyboard keyboard;
    private final PayEditText payEditText;
    private OnInputFinishListener mListener;

    @SuppressWarnings("deprecation")
    public InputSmsCodeDialog(Activity context, final SmsCodeListener getSmsCodeListener) {
        super(context, R.style.customDialog);
        view = LayoutInflater.from(context).inflate(R.layout.layout_pwd_keyboard, null);
        view.findViewById(R.id.iv_dismiss).setOnClickListener(this);
//        view.findViewById(R.id.tv_forget_pwd).setOnClickListener(this);

        final DTextView getSmsCode = (DTextView) view.findViewById(R.id.tv_get_phone_code);
        getSmsCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSmsCodeListener.getCode(getSmsCode);
            }
        });

        keyboard = (Keyboard) view.findViewById(R.id.keyboard);
        payEditText = (PayEditText) view.findViewById(R.id.payEditText);

        keyboard.setKeyboardKeys(KEY);
        initEvent();
        this.setContentView(view);
        this.setCancelable(true);
        Window dialogWindow = this.getWindow();
        if (null != dialogWindow) {
            dialogWindow.setGravity(Gravity.BOTTOM);
            dialogWindow.setWindowAnimations(R.style.AnimBottom);

            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = context.getWindowManager().getDefaultDisplay().getWidth();// 宽度全屏
            dialogWindow.setAttributes(lp);
        }
    }

    private void initEvent() {
        keyboard.setOnClickKeyboardListener(new Keyboard.OnClickKeyboardListener() {
            @Override
            public void onKeyClick(int position, String value) {
                if (position < 11 && position != 9) {
                    payEditText.add(value);
                } else if (position == 9) {
                    payEditText.remove();
                } else if (position == 11) {
                    //当点击完成的时候，也可以通过payEditText.getText()获取密码，此时不应该注册OnInputFinishedListener接口
                    String password = payEditText.getText();
                    if (null != password) {
                        if (password.length() == 6) {
                            notifyFinish(password);
                        }
                    }
                    dismiss();
                }
            }
        });

        /**
         * 当密码输入完成时的回调
         */
        payEditText.setOnInputFinishedListener(new PayEditText.OnInputFinishedListener() {
            @Override
            public void onInputFinished(String password) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        dismiss();
    }

    public interface OnInputFinishListener {
        void finish(String password);
    }

    public void setOnInputFinishListener(OnInputFinishListener listener) {
        this.mListener = listener;
    }

    public void notifyFinish(String password) {
        if (null != mListener) {
            mListener.finish(password);
        }
    }

    public interface SmsCodeListener {
        void getCode(TextView tv);
    }


}
